﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace GameHanhTrinh.FrameWork
{
    public enum GameStateID
    {
        STATE_MENU, STATE_NEWGAME, STATE_OPTION
    }

    public enum ScreenState
    {
        TransitionOn, Active, TransitionOff, Exiting, None
    }

    class CGameScreen
    {
        protected GameStateID _gameId;
        protected CGamePlay _gamePlay;
        protected CResourceManager _RM;

        protected TimeSpan _transitionOnTime = TimeSpan.Zero;
        protected TimeSpan _transitionOffTime = TimeSpan.Zero;

        protected float _transitionPosition = 0f;
        protected ScreenState _screenState = ScreenState.None;

        protected bool _isTransiting = false;
        protected bool _isVisible = true;
        protected bool _isInit = false;

        public CGameScreen(GameStateID gameId, CGamePlay gamePlay)
        {
            _gameId = gameId;
            _gamePlay = gamePlay;
        }

        public virtual void Init()
        {
            _isInit = true;
        }

        public virtual void Draw(SpriteBatch spriteBatch)
        {
        }

        /// <summary>
        /// Điều khiển transition position chuyển cảnh
        /// </summary>
        /// <param name="gameTime"></param>
        /// <param name="input"></param>
        public virtual void Update(GameTime gameTime, CInput input)
        {
            if (_screenState == ScreenState.TransitionOn)
            {
                _isTransiting = true;
                UpdateTransition(gameTime, _transitionOnTime, 1);
                if (_transitionPosition == 1)
                {
                    _screenState = ScreenState.Active;
                    _isTransiting = false;
                }
            }
            else if (_screenState == ScreenState.TransitionOff)
            {
                _isTransiting = true;
                UpdateTransition(gameTime, _transitionOffTime, -1);
                if (_transitionPosition == 0)
                {
                    ScreenExit();
                }
            }
        }

        /// <summary>
        /// Cập nhật vị trí đã chuyển được
        /// </summary>
        /// <param name="gameTime"></param>
        /// <param name="time">Thời gian chuyển cảnh</param>
        /// <param name="direction">1 để hiện (On), -1 để ẩn (Off)</param>
        public void UpdateTransition(GameTime gameTime, TimeSpan time, int direction)
        {
            
            float transitionDelta;

            if (time == TimeSpan.Zero)
                transitionDelta = 1;
            else
                transitionDelta = (float)(gameTime.ElapsedGameTime.TotalMilliseconds / time.TotalMilliseconds);

            _transitionPosition += transitionDelta * direction;
            if (_transitionPosition < 0 || _transitionPosition > 1)
                _transitionPosition = MathHelper.Clamp(_transitionPosition, 0, 1);
            
        }

        public virtual void Destroy()
        {
        }

        /// <summary>
        /// Thời gian hiệu ứng chuyển cảnh đến
        /// </summary>
        public TimeSpan TransitionOnTime
        {
            get { return _transitionOnTime; }
            set { _transitionOnTime = value; }
        }

        /// <summary>
        /// Thời gian chuyển cảnh đi
        /// </summary>
        public TimeSpan TransitionOffTime
        {
            get { return _transitionOffTime; }
            set { _transitionOffTime = value; }
        }

        /// <summary>
        /// Vị trí chuyễn cảnh. Giá trị từ 0 (hoàn toàn chưa hiển thị gì) đến 1 (hiển thị hoàn toàn)
        /// </summary>
        public float TransitionPosition
        {
            get { return _transitionPosition; }
            set { _transitionPosition = value; }
        }

        public void TransitionOn()
        {
            _screenState = ScreenState.TransitionOn;
            _isVisible = true;
        }

        public void TransitonOff()
        {
            _screenState = ScreenState.TransitionOff;
        }

        public void ScreenExit()
        {
            _screenState = ScreenState.Exiting;
            _isTransiting = false;
            _isVisible = false;
        }

        public ScreenState ScreenState
        {
            get { return _screenState; }
        }

        public bool IsInit
        {
            get { return _isInit; }
        }
    }
}
